<?php
require_once 'conexao.php';
?>
<html>
    <link rel="stylesheet" type="text/css" href="style.css"/>
    <body id="relatorio">
            <table align="left" border="1" cellpadding="1" cellspacing="1" style="width: 1210px; height: 93px;">

                <tr>
                    <td style="width: 200px; height: 70px;">
                        <img alt="" src="http://www.saoexpedito.com.br/imagens/logo.jpg" style="width: 201px; height: 70px;" /></td>
                    <td style="text-align: center;"><h1>CONTROLE DE ENTRADA E SAÍDA - FROTA</h1></td>
                    <td style="width: 150px;">
                        Rev.: 05<br />
                        Data: 09/11/2009<br />
                        P&aacute;gina 1 de 1</td>
                </tr>

            </table> 
            <p>
                <?php
                $datas = explode('/', $_POST['dt_entrada_ini']);
                $datade = $datas['2'] . "-" . $datas['1'] . "-" . $datas['0'];
                $datas = explode('/', $_POST['dt_entrada_fim']);
                $dataate = $datas['2'] . "-" . $datas['1'] . "-" . $datas['0'];
                $sql = "SELECT * FROM frota WHERE dt_entrada >='" . $datade . "' AND dt_entrada <= '" . $dataate . "'";

                if ($_POST['placa'] <> 'TODOS') {
                    $sql = $sql . " and placa = '" . $_POST['placa'] . "'";
                }
                if ($_POST['destino'] <> 'TODOS') {
                    $sql = $sql . " and destino = '" . $_POST['destino'] . "'";
                }
                if ($_POST['motorista_entrada'] <> 'TODOS' && $_POST['motorista_saida'] <> 'TODOS') {
                    $sql = $sql . " and (mot_entrada = '" . $_POST['motorista_entrada'] . "' or mot_saida = '" . $_POST['motorista_saida'] . "') ";
                } else {

                    if ($_POST['motorista_entrada'] <> 'TODOS') {
                        $sql = $sql . " and mot_entrada = '" . $_POST['motorista_entrada'] . "'";
                    }
                    if ($_POST['motorista_saida'] <> 'TODOS') {
                        $sql = $sql . " and mot_saida = '" . $_POST['motorista_saida'] . "'";
                    }
                }
                $sql = $sql . "ORDER BY dt_entrada";
                $result = mysql_query($sql);
                ?>
            </p>
            <p>&nbsp;</p>
            <p>&nbsp;</p>
            <p>&nbsp;</p>
            <p>&nbsp;</p>
            <table border='0' width="1210" name="tabela_rel">
                <tr style="background-color: lightgray; font-weight: bold">
                    <td colspan="4" align="center">ENTRADA</td>
                    <td rowspan="2" align="center">PLACA</td>
                    <td colspan="5" align="center">SAÍDA</td>
                </tr>
                
                <tr style="background-color: lightgray; font-weight: bold">
                    <td align="center">DATA</td>
                    <td align="center">MOTORISTA</td>
                    <td align="center">HORÁRIO</td>
                    <td align="center">USUÁRIO</td>
                    <td align="center">DATA</td>
                    <td align="center">MOTORISTA</td>
                    <td align="center">HORÁRIO</td>
                    <td align="center">DESTINO</td>
                    <td align="center">USUÁRIO</td>
                </tr>

                <?php
                echo mysql_error();
                while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                    $datas = explode('-', $row['dt_entrada']);
                    $datade = $datas['2'] . "/" . $datas['1'] . "/" . $datas['0'];
                    $datas = explode('-', $row['dt_saida']);
                    $dataate = $datas['2'] . "/" . $datas['1'] . "/" . $datas['0'];
                    if ($row['dt_saida'] == '0000-00-00') {
                        $row['mot_saida'] = '-';
                        $row['destino'] = '-';
                        $row['hr_saida'] = '-';
                        $row['port_saida'] = '-';
                        $dataate = '-';
                    }

                    echo"<tr>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . ($datade) .
                    "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . ($row['mot_entrada']) .
                    "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['hr_entrada'] .
                    "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['port_ent'] .
                    "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['placa'] .
                    "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $dataate .
                    "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['mot_saida'] .
                    "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['hr_saida'] .
                    "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['destino'] .
                    "</td> 
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['port_saida'] .
                    "</td> 
                    </tr>";
                }
                ?>
            </table>
    </body>
</html>